package com.smec;

import lombok.Data;

import java.text.DecimalFormat;

/**
 * @ClassName Complex
 * @Description TODO
 * @Author u3431
 * @Date 2021/12/27 15:38
 * @Version 1.0
 */

@Data
public class Complex {
    private float real;
    private float image;
    public void setReal(float real) {
        this.real = real;
    }
    public void setImage(float image) {
        this.image = image;
    }
    public float getReal() {
        return real;
    }
    public float getImage() {
        return image;
    }

    public Complex(){
        this.real = 0.0f;
        this.image = 0.0f;
    }
    public Complex(float real, float image) {
        this.real = real;
        this.image = image;
    }

    public Complex(float real) {
        this.real = real;
        this.image = 0.0f;
    }

    @Override
    public String toString() {
        if(image>0){
            return real + "+" + image + "i";
        }else if
        (image == 0){
            return String.valueOf(real);
        }
        else{
            return real + "-" + image*-1 + "i";
        }
    }

    public Complex add(Complex complexToAdd){
        this.real += complexToAdd.real;
        this.image += complexToAdd.image;
        return this;
    }
    public Complex subtract(Complex complexToSubtract){
        this.real -= complexToSubtract.real;
        this.image -= complexToSubtract.image;
        return this;
    }
    public Complex multiply(Complex complexToMultiply){
        float real = this.real * complexToMultiply.getReal() - this.image * complexToMultiply.getImage();
        float image = this.real * complexToMultiply.getImage() + this.image * complexToMultiply.getReal();
        this.real = real;
        this.image = image;
        return this;
    }

    public String abs(){
        float abs =(float) Math.sqrt(Math.abs(real * real)+ Math.abs(image * image));
        DecimalFormat df = new DecimalFormat(".00000000");
        return df.format(abs);
    }

    static public Complex multiply(Complex c1, Complex c2){
        Complex result = new Complex();
        result.setReal(c1.getReal() * c2.getReal() - c1.getImage() * c2.getImage());
        result.setImage(c1.getReal() * c2.getImage() + c1.getImage() * c2.getReal());
        return result;
    }



}
